<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
  Module: Upton::Utils
  
    &mdash; Documentation by YARD 0.8.7
  
</title>

  <link rel="stylesheet" href="../css/style.css" type="text/css" charset="utf-8" />

  <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8" />

<script type="text/javascript" charset="utf-8">
  hasFrames = window.top.frames.main ? true : false;
  relpath = '../';
  framesUrl = "../frames.html#!" + escape(window.location.href);
</script>


  <script type="text/javascript" charset="utf-8" src="../js/jquery.js"></script>

  <script type="text/javascript" charset="utf-8" src="../js/app.js"></script>


  </head>
  <body>
    <div id="header">
      <div id="menu">
  
    <a href="../_index.html">Index (U)</a> &raquo;
    <span class='title'><span class='object_link'><a href="../Upton.html" title="Upton (module)">Upton</a></span></span>
     &raquo; 
    <span class="title">Utils</span>
  

  <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
</div>

      <div id="search">
  
    <a class="full_list_link" id="class_list_link"
        href="../class_list.html">
      Class List
    </a>
  
    <a class="full_list_link" id="method_list_link"
        href="../method_list.html">
      Method List
    </a>
  
    <a class="full_list_link" id="file_list_link"
        href="../file_list.html">
      File List
    </a>
  
</div>
      <div class="clear"></div>
    </div>

    <iframe id="search_frame"></iframe>

    <div id="content"><h1>Module: Upton::Utils
  
  
  
</h1>

<dl class="box">
  
  
    
  
    
  
  
  
    <dt class="r1 last">Defined in:</dt>
    <dd class="r1 last">lib/utils.rb</dd>
  
</dl>
<div class="clear"></div>

<h2>Overview</h2><div class="docstring">
  <div class="discussion">
    
<p>This class contains a collection of helpers for Upton</p>

<p>Each method returns a Proc that (with an &amp; ) can be used as the final
argument to Upton&#39;s `scrape` and `scrape_to_csv`</p>


  </div>
</div>
<div class="tags">
  

</div>






  
    <h2>
      Class Method Summary
      <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
    </h2>

    <ul class="summary">
      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#list-class_method" title="list (class method)">+ (Object) <strong>list</strong>(list_selector, selector_method = :xpath) </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Scrapes any set of HTML elements into an Array.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#resolve_url-class_method" title="resolve_url (class method)">+ (Object) <strong>resolve_url</strong>(_href, _page_url) </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Takes :_href and resolves it to an absolute URL according to  the supplied
:_page_url.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#table-class_method" title="table (class method)">+ (Object) <strong>table</strong>(table_selector, selector_method = :xpath) </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Scrapes an HTML  element into an Array of Arrays.</p>
</div></span>
  
</li>

      
    </ul>
  



  <div id="class_method_details" class="method_details_list">
    <h2>Class Method Details</h2>

    
      <div class="method_details first">
  <h3 class="signature first" id="list-class_method">
  
    + (<tt>Object</tt>) <strong>list</strong>(list_selector, selector_method = :xpath) 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Scrapes any set of HTML elements into an Array.</p>


  </div>
</div>
<div class="tags">
  

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


35
36
37
38
39
40</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/utils.rb', line 35</span>

<span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_list'>list</span><span class='lparen'>(</span><span class='id identifier rubyid_list_selector'>list_selector</span><span class='comma'>,</span> <span class='id identifier rubyid_selector_method'>selector_method</span><span class='op'>=</span><span class='symbol'>:xpath</span><span class='rparen'>)</span>
  <span class='kw'>return</span> <span class='const'>Proc</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_instance_html'>instance_html</span><span class='op'>|</span>
    <span class='id identifier rubyid_html'>html</span> <span class='op'>=</span> <span class='op'>::</span><span class='const'>Nokogiri</span><span class='op'>::</span><span class='const'>HTML</span><span class='lparen'>(</span><span class='id identifier rubyid_instance_html'>instance_html</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_html'>html</span><span class='period'>.</span><span class='id identifier rubyid_send'>send</span><span class='lparen'>(</span><span class='id identifier rubyid_selector_method'>selector_method</span><span class='comma'>,</span> <span class='id identifier rubyid_list_selector'>list_selector</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span><span class='lbrace'>{</span><span class='op'>|</span><span class='id identifier rubyid_list_element'>list_element</span><span class='op'>|</span> <span class='id identifier rubyid_list_element'>list_element</span><span class='period'>.</span><span class='id identifier rubyid_text'>text</span> <span class='rbrace'>}</span>
  <span class='kw'>end</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="resolve_url-class_method">
  
    + (<tt>Object</tt>) <strong>resolve_url</strong>(_href, _page_url) 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Takes :_href and resolves it to an absolute URL according to</p>

<pre class="code ruby"><code class="ruby">the supplied :_page_url. They can be either Strings or URI
instances.</code></pre>

<p>raises ArgumentError if either href or page_url is nil raises ArgumentError
if page_url is not absolute</p>

<p>returns: a String with absolute URL</p>


  </div>
</div>
<div class="tags">
  
<p class="tag_title">Raises:</p>
<ul class="raise">
  
    <li>
      
      
        <span class='type'>(<tt>ArgumentError</tt>)</span>
      
      
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/utils.rb', line 51</span>

<span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_resolve_url'>resolve_url</span><span class='lparen'>(</span><span class='id identifier rubyid__href'>_href</span><span class='comma'>,</span> <span class='id identifier rubyid__page_url'>_page_url</span><span class='rparen'>)</span>
  
  <span class='id identifier rubyid_page_url'>page_url</span> <span class='op'>=</span> <span class='const'>URI</span><span class='lparen'>(</span><span class='id identifier rubyid__page_url'>_page_url</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_dup'>dup</span>
  <span class='id identifier rubyid_raise'>raise</span> <span class='const'>ArgumentError</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_page_url'>page_url</span><span class='embexpr_end'>}</span><span class='tstring_content'> must be absolute</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>unless</span> <span class='id identifier rubyid_page_url'>page_url</span><span class='period'>.</span><span class='id identifier rubyid_absolute?'>absolute?</span>

  <span class='id identifier rubyid_href'>href</span> <span class='op'>=</span> <span class='const'>URI</span><span class='lparen'>(</span><span class='id identifier rubyid__href'>_href</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_dup'>dup</span>

  <span class='comment'># return :href if :href is already absolute
</span>  <span class='kw'>return</span> <span class='id identifier rubyid_href'>href</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span> <span class='kw'>if</span> <span class='id identifier rubyid_href'>href</span><span class='period'>.</span><span class='id identifier rubyid_absolute?'>absolute?</span>


  <span class='comment'># TODO: There may be edge cases worth considering
</span>  <span class='comment'># but this should handle the following non-absolute href possibilities:
</span>  <span class='comment'># //anothersite.com (keeps scheme, too!)
</span>  <span class='comment'># /root/dir
</span>  <span class='comment'># relative/dir
</span>  <span class='comment'># ?query=2
</span>  <span class='comment'># #bang
</span>
  <span class='const'>URI</span><span class='period'>.</span><span class='id identifier rubyid_join'>join</span><span class='lparen'>(</span><span class='id identifier rubyid_page_url'>page_url</span><span class='comma'>,</span> <span class='id identifier rubyid_href'>href</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span>    
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="table-class_method">
  
    + (<tt>Object</tt>) <strong>table</strong>(table_selector, selector_method = :xpath) 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Scrapes an HTML &lt;table&gt; element into an Array of Arrays. The header,
if present, is returned as the first row.</p>


  </div>
</div>
<div class="tags">
  

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


20
21
22
23
24
25
26
27
28
29
30</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/utils.rb', line 20</span>

<span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_table'>table</span><span class='lparen'>(</span><span class='id identifier rubyid_table_selector'>table_selector</span><span class='comma'>,</span> <span class='id identifier rubyid_selector_method'>selector_method</span><span class='op'>=</span><span class='symbol'>:xpath</span><span class='rparen'>)</span>
  <span class='kw'>return</span> <span class='const'>Proc</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_instance_html'>instance_html</span><span class='op'>|</span>
    <span class='id identifier rubyid_html'>html</span> <span class='op'>=</span> <span class='op'>::</span><span class='const'>Nokogiri</span><span class='op'>::</span><span class='const'>HTML</span><span class='lparen'>(</span><span class='id identifier rubyid_instance_html'>instance_html</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_output'>output</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
    <span class='id identifier rubyid_headers'>headers</span> <span class='op'>=</span> <span class='id identifier rubyid_html'>html</span><span class='period'>.</span><span class='id identifier rubyid_send'>send</span><span class='lparen'>(</span><span class='id identifier rubyid_selector_method'>selector_method</span><span class='comma'>,</span> <span class='id identifier rubyid_table_selector'>table_selector</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_css'>css</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>th</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span> <span class='op'>&amp;</span><span class='symbol'>:text</span>
    <span class='id identifier rubyid_output'>output</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_headers'>headers</span>

    <span class='id identifier rubyid_table'>table</span> <span class='op'>=</span> <span class='id identifier rubyid_html'>html</span><span class='period'>.</span><span class='id identifier rubyid_send'>send</span><span class='lparen'>(</span><span class='id identifier rubyid_selector_method'>selector_method</span><span class='comma'>,</span> <span class='id identifier rubyid_table_selector'>table_selector</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_css'>css</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>tr</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span><span class='lbrace'>{</span><span class='op'>|</span><span class='id identifier rubyid_tr'>tr</span><span class='op'>|</span> <span class='id identifier rubyid_output'>output</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_tr'>tr</span><span class='period'>.</span><span class='id identifier rubyid_css'>css</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>td</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='symbol'>:text</span><span class='rparen'>)</span> <span class='rbrace'>}</span>
    <span class='id identifier rubyid_output'>output</span>
  <span class='kw'>end</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
  </div>

</div>

    <div id="footer">
  Generated on Thu Aug 15 00:25:44 2013 by
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
  0.8.7 (ruby-2.0.0).
</div>

  </body>
</html>